﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;

namespace Tailoring
{
    public partial class frmentry : Form
    {
        private string clientID = "";
        public string psearchkey { set { clientID = value; } }
        public frmentry()
        {
            InitializeComponent();

        }

        public void loadclient(string a)
        {

            SqlConnection conn = new SqlConnection("Server= localhost; database=tailoringmng; integrated Security=true");
            try
            {
                conn.Open();
                string strquery = "select * from tblclientinfo where clientId=" + a;
                SqlCommand sqlcmd = new SqlCommand(strquery, conn);
                //sqlcmd.Parameters.AddWithValue("@id", txtid.Text);
                SqlDataReader sqldreader = sqlcmd.ExecuteReader();
                if (sqldreader.HasRows)
                {
                    while (sqldreader.Read())
                    {
                        lbstatus.ForeColor = Color.Red;
                        lbstatus.Text = "";
                        txtid.Text = sqldreader["clientId"].ToString();
                        txtname.Text = sqldreader["name"].ToString();
                        txtaddress.Text = sqldreader["address"].ToString();
                        txtcnic.Text = sqldreader["cnic"].ToString();
                        cmbcity.Text = sqldreader["city"].ToString();
                        txtmobile.Text = sqldreader["mobile"].ToString();
                    }
                }
                else
                {
                    MessageBox.Show("No Record Found For ID" + " " + clientID);
                }
                sqldreader.Close();
                conn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);

            }
        }
        static string UppercaseWords(string value)
        {
            char[] array = value.ToCharArray();
            // Handle the first letter in the string.
            if (array.Length >= 1)
            {
                if (char.IsLower(array[0]))
                {
                    array[0] = char.ToUpper(array[0]);
                }
            }
            // Scan through the letters, checking for spaces.
            // ... Uppercase the lowercase letters following spaces.
            for (int i = 1; i < array.Length; i++)
            {
                if (array[i - 1] == ' ')
                {
                    if (char.IsLower(array[i]))
                    {
                        array[i] = char.ToUpper(array[i]);
                    }
                }
            }
            return new string(array);
        }
        //protected void LoadImage()
        //{

        //    try
        //    {
        //        this.openFileDialog1.ShowDialog(this);
        //        string strFn = this.openFileDialog1.FileName;
        //        this.pictureBox1.Image = Image.FromFile(strFn);
        //        FileInfo fiImage = new FileInfo(strFn);
        //        this.m_lImageFileLength = fiImage.Length;
        //        FileStream fs = new FileStream(strFn, FileMode.Open, FileAccess.Read, FileShare.Read);
        //        m_barrImg = new byte[Convert.ToInt32(this.m_lImageFileLength)];
        //        int iBytesRead = fs.Read(m_barrImg, 0, Convert.ToInt32(this.m_lImageFileLength));
        //        fs.Close();

        //    }
        //    catch (Exception ex)
        //    {
        //        MessageBox.Show(ex.Message);
        //    }
        void cleartxtfield()
        {
            txtid.Text = "";
            txtname.Text = "";
            txtmobile.Text = "";
            txtcnic.Text = "";
            txtaddress.Text = "";
            cmbcity.Text = "";

            txtlambai.Text = "";
            txtchatti.Text = "";
            txtcollar.Text = "";
            txtasteen.Text = "";
            txttera.Text = "";
            txtdaman.Text = "";
            txtshalwar.Text = "";
            txtpancha.Text = "";
            txtid.Focus();




        }

        public string pid { get { return txtid.Text; } }
        public string pname { get { return txtname.Text; } }
        public string pmobile { get { return txtmobile.Text; } }
        public string pcnic { get { return txtcnic.Text; } }
        public string pcity { get { return cmbcity.Text; } }
        public string paddress { set { txtaddress.Text = value; } get { return txtaddress.Text; } }
        public string pchati
        {
            get { return txtchatti.Text; }
        }
        public string pdaman
        {
            get { return txtdaman.Text; }
        }

        public string plambai
        {
            get { return txtlambai.Text; }
        }
        public string pshalwar
        {
            get { return txtshalwar.Text; }
        }
        public string pasteen
        {
            get { return txtasteen.Text; }
        }
        public string ptera
        {
            get { return txttera.Text; }
        }
        public string ppansa
        {
            get { return txtpancha.Text; }
        }
        public string pcollor
        {
            get { return txtcollar.Text; }
        }

        private void textBox5_Enter(object sender, EventArgs e)
        {
            Tera.Visible = false;
            Shalwar.Visible = true;
            chatti.Visible = false;
            shal1.Visible = false;
            daman.Visible = false;
            pancha.Visible = false;
            collar.Visible = false;
            asteen.Visible = false;
            normal.Visible = false;
        }

        private void textBox6_Enter(object sender, EventArgs e)
        {
            Tera.Visible = true;
            Shalwar.Visible = false;
            chatti.Visible = false;
            shal1.Visible = false;
            daman.Visible = false;
            pancha.Visible = false;
            collar.Visible = false;
            asteen.Visible = false;
            normal.Visible = false;
        }

        private void textBox7_Enter(object sender, EventArgs e)
        {
            Shalwar.Visible = false;
            Tera.Visible = false;
            shal1.Visible = false;
            chatti.Visible = false;
            daman.Visible = false;
            pancha.Visible = false;
            collar.Visible = false;
            asteen.Visible = true;
            normal.Visible = false;
        }

        private void textBox8_Enter(object sender, EventArgs e)
        {
            Shalwar.Visible = false;
            shal1.Visible = false;
            chatti.Visible = false;
            Tera.Visible = false;
            daman.Visible = false;
            pancha.Visible = false;
            collar.Visible = true;
            asteen.Visible = false;
            normal.Visible = false;
        }

        private void textBox9_Enter(object sender, EventArgs e)
        {
            Shalwar.Visible = false;
            shal1.Visible = false;
            chatti.Visible = true;
            Tera.Visible = false;
            daman.Visible = false;
            pancha.Visible = false;
            collar.Visible = false;
            asteen.Visible = false;
            normal.Visible = false;
        }

        private void textBox10_Enter(object sender, EventArgs e)
        {
            Shalwar.Visible = false;
            chatti.Visible = false;
            daman.Visible = true;
            pancha.Visible = false;
            Tera.Visible = false;
            shal1.Visible = false;
            collar.Visible = false;
            asteen.Visible = false;
            normal.Visible = false;
        }

        private void textBox12_Enter(object sender, EventArgs e)
        {
            Shalwar.Visible = false;
            shal1.Visible = false;
            chatti.Visible = false;
            daman.Visible = false;
            Tera.Visible = false;
            pancha.Visible = true;
            collar.Visible = false;
            asteen.Visible = false;
            normal.Visible = false;
        }

        private void textBox11_Enter(object sender, EventArgs e)
        {
            shal1.Visible = true;
            Shalwar.Visible = false;
            chatti.Visible = false;
            daman.Visible = false;
            Tera.Visible = false;
            pancha.Visible = false;
            collar.Visible = false;
            asteen.Visible = false;
            normal.Visible = false;
        }

        private void shal1_Click(object sender, EventArgs e)
        {

        }

        private void Form1_MouseHover(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            //SqlConnection sqlconn = new SqlConnection();
            //sqlconn = programcodes.getConnect();


            //try
            //{


            //    sqlconn.Open();
            //    if (sqlCommand1.Parameters.Count ==0 )
            //    {
            //        this.sqlCommand1.CommandText="INSERT INTO tblImgData(ID,Name,Picture) values(@ID,@Name,@Picture)";
            //        this.sqlCommand1.Parameters.Add("@ID",System.Data.SqlDbType.Int,4);
            //        this.sqlCommand1.Parameters.Add("@Name",System.Data.SqlDbType.VarChar,50);
            //        this.sqlCommand1.Parameters.Add("@Picture",System.Data.SqlDbType.Image);
            //    }
            //    this.sqlCommand1.Parameters["@ID"].Value=this.txtid.Text;
            //    this.sqlCommand1.Parameters["@Name"].Value=this.txtname.Text;
            //    this.sqlCommand1.Parameters["@Picture"].Value=this.m_barrImg;



            //int iresult=this.sqlCommand1.ExecuteNonQuery();
            //    MessageBox.Show(Convert.ToString(iresult));								

            //}
            //catch(Exception ex)
            //{
            //    MessageBox.Show(ex.Message);

            //}
            //finally
            //{
            //    this.sqlconn.Close();

            //}
        }




        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            txtid.Focus();
            //SqlConnection conn = new SqlConnection();
            //conn = programcodes.getConnect();
            //conn.Open();

        }

        private void button3_Click(object sender, EventArgs e)
        {

            //LoadImage();
            //programcodes objpc = new programcodes();
            //PictureBox pb = new PictureBox();
            //objpc.LoadImage(pb);

            //pictureBox2 = pb;

            //OpenFileDialog odb = new OpenFileDialog();
            //odb.Filter = "Image Files(*.jpg;*.jpeg;*.png;*.gif;*.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";
            //if (odb.ShowDialog() == DialogResult.OK)
            //{
            //pictureBox2.Image = new Bitmap(odb.FileName);

            //}
        }

        private void txtlambai_KeyPress(object sender, KeyPressEventArgs e)
        {
            {
                if (!char.IsControl(e.KeyChar)
                    && !char.IsDigit(e.KeyChar)
                    && e.KeyChar != '.')
                {
                    e.Handled = true;
                }
            }




        }

        private void txtid_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar)
                    && !char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtmobile_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar)
                    && !char.IsDigit(e.KeyChar)
                && e.KeyChar != '-')
            {
                e.Handled = true;
            }
        }

        private void txtcnic_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar)
                  && !char.IsDigit(e.KeyChar) && e.KeyChar != '-')
            {
                e.Handled = true;
            }
        }

        private void txtcnic_TextChanged(object sender, EventArgs e)
        {
            string cn = txtcnic.Text;
            if (cn.Length > 15)
            {
                toolTip1.IsBalloon = true;
                toolTip1.ToolTipIcon = ToolTipIcon.Info;
                toolTip1.ToolTipTitle = "Somthing Wrong with Data Entry";
                toolTip1.Show("Only two - and 13 Digits are Allowed Select the Text and Delete And Try Again !", txtcnic, 0, -80, 2500);
            }
        }

        private void txtid_Leave(object sender, EventArgs e)
        {
            if (txtid.Text.Length > 0 && txtname.Text.Length > 0)
            {
                //lbedit.Visible = true;
            }
            SqlConnection conn = new SqlConnection("Server= localhost; database=tailoringmng; integrated Security=true");
            try
            {
                conn.Open();
                string strquery = "select * from tblclientinfo where clientId=" + txtid.Text;
                SqlCommand sqlcmd = new SqlCommand(strquery, conn);
                //sqlcmd.Parameters.AddWithValue("@id", txtid.Text);
                SqlDataReader sqldreader = sqlcmd.ExecuteReader();
                if (sqldreader.HasRows)
                {

                    txtname.Enabled = false;
                    savemnpemaish.Enabled = false;
                    txtaddress.Enabled = false;
                    txtcnic.Enabled = false;
                    cmbcity.Enabled = false;
                    txtmobile.Enabled = false;

                    lbstatus.ForeColor = Color.Red;
                    lbstatus.Text = "This ID Exist Try Another One";

                }
                else
                {
                    txtname.Enabled = true;
                    savemnpemaish.Enabled = true;
                    txtaddress.Enabled = true;
                    txtcnic.Enabled = true;
                    cmbcity.Enabled = true;
                    txtmobile.Enabled = true;
                    
                    lbstatus.ForeColor = Color.Green;
                    lbstatus.Text = "This ID is Available";
                }
                sqldreader.Close();
            


            }
            catch
            {
                // MessageBox.Show(ex.Message);
            }
            finally { conn.Close(); }
        }

        private void savemnpemaish_Click(object sender, EventArgs e)
        {
            if (txtid.Text != "" && txtname.Text != "" && txtmobile.Text != "" && txtlambai.Text != "" && txtshalwar.Text != "")
            {
                frmconfirm frm = new frmconfirm();
                string cname = UppercaseWords(txtname.Text);


                frm.pid = pid;
                frm.pname = cname;
                frm.pmobile = pmobile;
                frm.pcnic = pcnic;
                frm.paddress = paddress;
                frm.pchati = pchati;
                frm.plambai = plambai;
                frm.pdaman = pdaman;
                frm.pasteen = pasteen;
                frm.ptera = ptera;
                frm.pcollor = pcollor;
                frm.pshalwar = pshalwar;
                frm.ppansa = ppansa;
                frm.pcity = pcity;
                frm.ShowDialog();
                cleartxtfield();
            }
            else
                MessageBox.Show("No Data Present to Save !");
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if ((txtid.Text.Length > 0) || (txtname.Text.Length > 0) || (txtmobile.Text.Length > 0))
            {
                if (MessageBox.Show("Are you Sure to Close the Form?",
                          "Pemaish  Application Closing Confirmation",
                           MessageBoxButtons.YesNo,
                           MessageBoxIcon.Information) == DialogResult.Yes)
                {
                    this.Close();
                }

            }
            else
            {
                this.Close();
            }
        }

        private void editToolStripMenuItem1_Click(object sender, EventArgs e)
        {

            if (txtid.Text.Length > 0)
            {
                frmviewpemaish vp = new frmviewpemaish();
                vp.psearchkey = txtid.Text;
                vp.MdiParent = this.ParentForm;

                vp.Show();
                string str = "SELECT     dbo.tblclientinfo.*, dbo.tblpemaish.* FROM dbo.tblpemaish INNER JOIN dbo.tblclientinfo ON dbo.tblpemaish.clientId = dbo.tblclientinfo.clientId WHERE(dbo.tblclientinfo.clientId) =@Key";
                vp.loadclient(txtid.Text, str);
                
                vp.enblecontrol();
                this.Close();
            }
            else
            {
                Searchpopup spop = new Searchpopup();
                spop.MdiParent = this.ParentForm;
                spop.Show();
                this.Close();
            }
            //loadclient(clientID);
            //this.Close();
        }

        private void lbedit_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {

            //txtname.Enabled = true;
            //txtaddress.Enabled = true;
            //txtcnic.Enabled = true;
            //cmbcity.Enabled = true;
            //txtmobile.Enabled = true;
            //lbedit.Visible = false;
            //txtid.Enabled = false;
            //lbstatus.Text = "You Can't Modify Key ";
        }

        private void txtid_TextChanged(object sender, EventArgs e)
        {
            txtname.Enabled = true;
            txtaddress.Enabled = true;
            txtcnic.Enabled = true;
            cmbcity.Enabled = true;
            txtmobile.Enabled = true;
            
            lbstatus.Text = "";
        }

        private void txtlambai_Validating(object sender, CancelEventArgs e)
        {
            try
            {
                decimal lambai = Convert.ToDecimal(txtlambai.Text);
                if (lambai < 13 || lambai > 50)
                {

                    e.Cancel = true;
                    txtlambai.Select(0, txtlambai.Text.Length);


                    this.errorProvider1.SetError(txtlambai, "Values Limit Exceded Specify value Between 13 And 50");

                }
            }
            catch (Exception ex) { MessageBox.Show("Error : " + ex.Message + "Collar Field is Either Empty or Something Wrong With Data Input"); }
        }

        private void txtlambai_Validated(object sender, EventArgs e)
        {
            
            errorProvider1.SetError(txtlambai, "");
        }

        private void txtchatti_Validating(object sender, CancelEventArgs e)
        {
            try
            {
                decimal chatti = Convert.ToInt32(txtchatti.Text);
                if (chatti < 18 || chatti > 45)
                {

                    e.Cancel = true;
                    txtchatti.Select(0, txtchatti.Text.Length);


                    this.errorProvider1.SetError(txtchatti, "Values Limit Exceded Specify value Between 18 And 45");

                }
            }
            catch (Exception ex) { MessageBox.Show("Error : " + ex.Message + "Chatti Field is Either Empty or Something Wrong With Data Input"); }
        }

        private void txtchatti_Validated(object sender, EventArgs e)
        {
            errorProvider1.SetError(txtchatti, "");
        }

        private void txtcollar_Validated(object sender, EventArgs e)
        {
            errorProvider1.SetError(txtcollar, "");
        }

        private void txtcollar_Validating(object sender, CancelEventArgs e)
        {
            try
            {
                decimal collar = Convert.ToInt32(txtcollar.Text);
                if (collar < 9 || collar > 25)
                {

                    e.Cancel = true;
                    txtcollar.Select(0, txtcollar.Text.Length);


                    this.errorProvider1.SetError(txtcollar, "Values Limit Exceded Specify value between 9 and 25");

                }
            }
            catch (Exception ex) { MessageBox.Show("Error : "+ex.Message+"Collar Field is Either Empty or Something Wrong With Data Input"); }

        }

        private void txtasteen_Validating(object sender, CancelEventArgs e)
        {
            try
            {
                decimal asteen = Convert.ToInt32(txtasteen.Text);
                if (asteen < 8 || asteen > 28)
                {

                    e.Cancel = true;
                    txtasteen.Select(0, txtasteen.Text.Length);


                    this.errorProvider1.SetError(txtasteen, "Values Limit Exceded Specify value between 8 and 25");

                }
            }
            catch { }
        }

        private void txtasteen_Validated(object sender, EventArgs e)
        {
            this.errorProvider1.SetError(txtasteen, "");
        }

        private void txttera_Validating(object sender, CancelEventArgs e)
        {
            try
            {
                decimal tera = Convert.ToInt32(txttera.Text);
                if (tera < 8 || tera > 23)
                {

                    e.Cancel = true;
                    txttera.Select(0, txttera.Text.Length);


                    this.errorProvider1.SetError(txttera, "Values Limit Exceded Specify value between 8 and 23");

                }
            }
            catch { }
        }

        private void txttera_Validated(object sender, EventArgs e)
        {
            this.errorProvider1.SetError(txttera, "");
        }

        private void txtdaman_Validating(object sender, CancelEventArgs e)
        {
            try
            {
                decimal daman = Convert.ToInt32(txtdaman.Text);
                if (daman < 10 || daman > 35)
                {

                    e.Cancel = true;
                    txtdaman.Select(0, txtdaman.Text.Length);


                    this.errorProvider1.SetError(txtdaman, "Values Limit Exceded Specify value between 10 and 35");


                }
            }
            catch { }

        }

        private void txtdaman_Validated(object sender, EventArgs e)
        {
            this.errorProvider1.SetError(txtdaman, "");
        }

        private void txtpancha_Validating(object sender, CancelEventArgs e)
        {
            try
            {
                decimal pancha = Convert.ToInt32(txtpancha.Text);
                if (pancha < 7 || pancha > 15)
                {

                    e.Cancel = true;
                    txtpancha.Select(0, txtpancha.Text.Length);


                    this.errorProvider1.SetError(txtpancha, "Values Limit Exceded Specify value between 7 and 15");

                }
            }
            catch { }
        }

        private void txtpancha_Validated(object sender, EventArgs e)
        {
            this.errorProvider1.SetError(txtpancha, "");
        }

        private void txtshalwar_Validating(object sender, CancelEventArgs e)
        {
            try
            {
                decimal shalwar = Convert.ToInt32(txtshalwar.Text);

                if (shalwar < 10 || shalwar > 45)
                {

                    e.Cancel = true;
                    txtshalwar.Select(0, txtshalwar.Text.Length);


                    this.errorProvider1.SetError(txtshalwar, "Values Limit Exceded Specify value between 10 and 45");

                }
            }
            catch {  }
        }
        
        

        private void txtshalwar_Validated(object sender, EventArgs e)
        {
            this.errorProvider1.SetError(txtshalwar, "");
        }

        private void txtmobile_Validating(object sender, CancelEventArgs e)
        {
             try
            {
              

                if (txtmobile.Text.Length>11)
                {

                    e.Cancel = true;
                    txtshalwar.Select(0, txtmobile.Text.Length);


                    this.errorProvider1.SetError(txtmobile, "Mobile Number is Of Mazimum 11 Digits !!");

                }
            }
            catch {  }
        }

        private void txtmobile_Validated(object sender, EventArgs e)
        {
             this.errorProvider1.SetError(txtmobile, "");
        }
        


    }
}
